package org.six11.ss.server.web;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.six11.util.adt.MultiState;

/**
 * This is the base class for all the HTML page (JSP actually) handlers. The WebHandlerServlet
 * figures out which of these should be invoked, and then calls its 'service' method, which is
 * abstract in WebHandler and must be implemented by the subclass.
 * 
 * All subclasses have access to the protected 'state' member, which is the big hash table for
 * storing things that should be persistent or shared among many connections.
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public abstract class WebHandler {

  protected MultiState state;
  private String jspPage;

  public WebHandler(String jspPage, MultiState state) {
    this.jspPage = jspPage;
    this.state = state;
  }

  protected abstract void service(HttpServletRequest req, HttpServletResponse resp,
      ServletContext context) throws ServletException, IOException;

  protected void go(HttpServletRequest req, HttpServletResponse resp, ServletContext context)
      throws ServletException, IOException {
    forwardTo(jspPage, req, resp, context);
  }

  protected void forwardTo(String servletPath, HttpServletRequest req, HttpServletResponse resp,
      ServletContext context) throws ServletException, IOException {
    context.getRequestDispatcher(servletPath).forward(req, resp);
  }
}
